<?php
include '../../../wp-blog-header.php';
date_default_timezone_set('Asia/Kolkata');
$today = date("Ymd").'T120000'."<br />";
$yesterday  = date('Ymd',strtotime('-1 day',strtotime(date("Ymd")))).'T120000';
$url = 'http://buzzr:password@api.goibibo.ibibo.com/api/deals/search/created/'.$yesterday.'-TO-'.$today.'?format=json';
//$url = 'http://buzzr:password@api.goibibo.ibibo.com/api/deals/search/created/20101027T120000-TO-20101028T120000?format=json';
//echo $url;
$brand_id = '246';
exit();

function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$json_decoded = json_decode($curl_json_output, true);


echo "<pre>"; 
print_r($json_decoded['data']);
//exit();

if(empty($json_decoded['data']))
{
  $url = 'http://buzzr:password@api.goibibo.ibibo.com/api/deals/search/updated/'.$yesterday.'-TO-'.$today.'?format=json';
  $curl_json_output = download_page($url);
  $json_decoded = json_decode($curl_json_output, true);
}

echo "<pre>"; 
print_r($json_decoded['data']);
//exit();


$url = 'http://buzzr:password@api.goibibo.ibibo.com/api/deals/description/code/'.$json_decoded['data']['6']['code'].'?format=json';
$curl_json_output = download_page($url);
$json_decoded = json_decode($curl_json_output, true);

/*
echo "<pre>"; 
print_r($json_decoded['data']);
*/

//echo $url;
 
//print_r($json_decoded['data']);

//exit();


if(!empty($json_decoded['data']))
{
  //$highlights = $json_decoded['data'][0]['description'][0]['highlights'];
  //preg_match("/(<li>).+?(?=<\/li>)/", $highlights, $matches_new);
  
  //$columns_deal['name'] = pods_sanitize(ucwords(strtolower(strip_tags($matches_new[0]))));
  
  
  $columns_deal['name'] = ucwords(strtolower($json_decoded['data']['0']['name']));
  $columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
  
  $Record = new Pod('deal');
  $where = 'camp_link = "'.$columns_deal['camp_link'].'"';
  $Record->findRecords('id DESC', '',$where);
  $dealsFound = $Record->getTotalRows();
  echo $dealsFound;
  //exit();
  if($dealsFound == 0)
  {
    if(strtotime($json_decoded['data']['0']['exp_date']) > strtotime(date('Ymd')))
    {
    /* In an array get all the current tags */
  	$City = new Pod('city');
  	$City->findRecords('name ASC', 1000);
  	while($City->fetchRecord())
  	{
    	$ids  = $City->get_field('id');
    	$current_cities[] = $ids;
  	}
    $current_cities = implode(',',$current_cities);
  
  
    date_default_timezone_set('Asia/Kolkata');
    $columns_deal['brand'] = $brand_id;
    
    $link = 'http://ads.ibibo.com/ad/www/delivery/ck.php?oaparams=2__bannerid=1459__zoneid=7753__OXLCA=1__cb=53b8530c90__oadest=http%3A%2F%2Fgoibibo.ibibo.com%2Fdeals%2Fproduct%2F'.$json_decoded['data']['0']['code'].'%2F';
    
    $shortened_url = make_bitly_url($link);
    if(empty($shortened_url))
    {
      $shortened_url = make_goog_url($link);
    }
    $columns_deal['extlink'] = $shortened_url;
    
    if(date('Ymd',strtotime($json_decoded['data']['0']['exp_date'])) > date('Ymd',strtotime('+2 day',strtotime(date("Ymd")))))
    {
      $columns_deal['camp_end'] =  date('Y-m-d H:i:s',strtotime('+2 day',strtotime(date("Y-m-d 12:00:00"))));
    }
    else
    {
      $columns_deal['camp_end'] =  $json_decoded['data']['0']['exp_date'];
    }
    
    $columns_deal['camp_start'] = date('Y-m-d H:i:s');
    $columns_deal['daily'] = '1';
    $columns_deal['oriprice'] = floor($json_decoded['data']['0']['original_price']);
    
    $discount_pecentage = floor(((($json_decoded['data']['0']['original_price'])-($json_decoded['data']['0']['price']))/($json_decoded['data']['0']['original_price']))*100);
    $columns_deal['disper'] = $discount_pecentage;
    
    $columns_deal['disprice'] = floor($json_decoded['data']['0']['price']);
    $columns_deal['ext_image_url'] = $json_decoded['data'][0]['description'][0]['image2'];
    $columns_deal['city'] = $current_cities;
    $columns_deal['camp_cat'] = '5';//Travel
    
    $columns_deal['short'] = $json_decoded['data']['0']['code']; 
    /*
    echo '<pre>';
    print_r($columns_deal);
    echo '</pre>';
    */
    $api = new PodAPI();
    
    $params = array('datatype' => 'deal', 'columns' => $columns_deal);
    
    
    echo '<pre>';
    print_r($params);
    echo '</pre>';
    //exit();
    

    $api->save_pod_item($params);
   } 
  }
  /*
  else
  {
    echo "Feed has No New Deals Thank You!";
  }
  */
}
/*
else
{
  echo "Feed has No New Deals Thank You!";
}
*/
?>
